<template>
  <!--  添加用户与修改页面-->
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? '新增用户' : '编辑用户'" :close-on-click-modal="false"
             :close-on-press-escape="false" @close="cancellation">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()"
             label-width="120px">
      <el-form-item prop="parentId" label="推广人员">
        <el-input v-model="dataForm.parentId" placeholder="请输入推广人员" clearable></el-input>
      </el-form-item>
      <el-form-item prop="identity" label="用户身份">
        <ren-radio-group v-model="dataForm.identity" dict-type="identity"></ren-radio-group>
      </el-form-item>
<!--      <el-form-item prop="availablePointsNum" label="可用积分">-->
<!--        <el-input-number v-model="dataForm.availablePointsNum" :precision="2" :min="0"></el-input-number>-->
<!--      </el-form-item>-->
      <el-form-item prop="availableEdouNum" label="可用余额">
        <el-input-number v-model="dataForm.availableEdouNum" :precision="2" :min="0"></el-input-number>
      </el-form-item>
      <el-form-item label="所属区域">
        <el-cascader
            v-model="value"
            :options="options"
            :props="{value:'name',label:'name',children:'children'}"
            filterable></el-cascader>
      </el-form-item>
<!--      <el-form-item prop="deptName" label="所属分公司">-->
<!--      <ren-dept-tree v-model="dataForm.deptId" placeholder="请选择所属分公司" :dept-name.sync="dataForm.deptName"></ren-dept-tree>-->
<!--      </el-form-item>-->
<!--      <el-form-item prop="isManager" label="分公司负责人">-->
<!--        <ren-radio-group v-model="dataForm.isManager" dict-type="isManager"></ren-radio-group>-->
<!--      </el-form-item>-->
    </el-form>
    <template slot="footer">
      <el-button @click="cancellation">取消</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">确认</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from 'lodash/debounce'
import addressData from "@/utils/address-data"; //引入省市区数据

export default {
  data() {
    return {
      visible: false,
      productList: [],                //商品列表
      options:addressData,            //地址列表
      value: [],                      //选中地址
      dataForm: {
        id: '',                       //id
        identity: '',                 //2+1用户身份0用户1会员2合伙人
        availableEdouNum: '',       	//奖励
        availablePointsNum: '',       //积分
        performance: '',              //业绩
        ranking: '',                  //排名
        bankName: '',	                //收款银行
        bankNumber: '',               //收款人账号
        bankUsername: '',             //收款人姓名
        avatar: '',                   //用户头像
        mobile: '',                   //手机号
        nickname: '',                 //昵称
        openid: '',                   //openid
        parentId: '',                 //父级ID
        username: '',                 //用户名
        deptId:'',                    //部门ID
        deptName:'',                  //部门名称
        isManager:'',                 //是否为分公司负责人
        createDateStart: '',          //开始创建时间
        createDateEnd: '',            //结束创建时间
        province: '',                 //省
        city: '',                     //市
        area: '',                     //区县
      }
    }
  },
  computed: {
    dataRule() {
      return {
        areaIdentity: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        identity: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        // deptName: [
        //   {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        // ]
        province: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        city: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        area: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
      }
    }
  },
  methods: {
    init() {
      this.visible = true
      this.dataForm.deptId = ''
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        this.getDeptList()
        this.value = []
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    //取消
    cancellation(){
      this.visible = false
      this.$emit('cancellation')
    },
    // 获取信息
    getInfo() {
      this.$http.get(`/api/tbUser/${this.dataForm.id}`).then(({data: res}) => {
        // console.log(res)
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
        this.value = []
        if (res.data.province){
          this.value.push(res.data.province,res.data.city,res.data.area)
        }
      }).catch(() => {
      })
    },
    // 获取公司信息
    getDeptList() {
      this.$http.get(`/sys/dept/list`).then(({data: res}) => {
        //console.log(res)
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.deptList =res.data
      }).catch(() => {
      })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        if (this.value.length!==0){
          this.dataForm['province'] =this.value[0]
          this.dataForm['city'] =this.value[1]
          this.dataForm['area'] =this.value[2]
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/api/tbUser', this.dataForm).then(({data: res}) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {
        })
      })
    }, 1000, {'leading': true, 'trailing': false}),
  }
}
</script>
